package cn.wyf.myaidlserver.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import net.wyf.myaidlserver.ICallListener;
import net.wyf.myaidlserver.IMyAidlInterface;
import net.wyf.myaidlserver.User;

public class MyService extends Service {

    private static final String TAG = "AIDL";
    private ICallListener mListener;

    private Binder mBinder = new IMyAidlInterface.Stub() {
        @Override
        public double add(double a, double b) throws RemoteException {
            Log.d(TAG, "a = " + a + ",b = " + b);
            return a + b;
        }

        @Override
        public void testLister() throws RemoteException {
            if(null != mListener){

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Thread.sleep(5000);
                            mListener.onMessage("test success");
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }

        @Override
        public String inUserInfo(User user) throws RemoteException {
            String msg = "table1" + "\n" + "----------------------------------------------" + "\n" + "|" +
                    " code " + "|" + " " +
                    "message " + "\n" +
                    "----------------------------------------------" + "\n" + "|  " + user.getrCode() + " " +
                    "|  " + user.getrMessage() + "\n" + "----------------------------------------------";
            return msg;
        }

        @Override
        public String outUserInfo(User user) throws RemoteException {
            String msg = "table2" + "\n" + "----------------------------------------------" + "\n" + "|" +
                    " code " + "|" + " " +
                    "message " + "\n" +
                    "----------------------------------------------" + "\n" + "|  " + user.getrCode() + " " +
                    "|  " + user.getrMessage() + "   | " +
                    "\n" + "----------------------------------------------";
            return msg;
        }

        @Override
        public String inOutUserInfo(User user) throws RemoteException {
            String msg = "table3" + "\n" + "----------------------------------------------" + "\n" + "|" +
                    " code " + "|" + " " +
                    "message " + "\n" +
                    "----------------------------------------------" + "\n" + "|  " + user.getrCode() + " " +
                    "|  " + user.getrMessage() + "   | " +
                    "\n" + "----------------------------------------------";
            return msg;
        }

        @Override
        public User getData() throws RemoteException {
            User user = new User(4, "ww");
            return user;
        }

        @Override
        public void registListener(ICallListener listener) throws RemoteException {
            mListener = listener;
        }

        @Override
        public void unRegistLitener(ICallListener listener) throws RemoteException {
            mListener = null;
        }

    };

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        return mBinder;
    }
}
